package com.nut.course.demo;

import org.openjdk.jol.info.ClassLayout;
import org.openjdk.jol.vm.VM;

import java.util.concurrent.CountDownLatch;


public class JOLDemo {


    /**
     * 0  01 无锁
     * 1  01 偏向锁
     *    00 轻量级锁
     *    10 重量级锁
     *    11 GC标记
     * @param args
     * @throws InterruptedException
     */
    public static void main(String[] args) throws InterruptedException {

//        Thread.sleep(5000);

        A a = new A();

        System.out.println(VM.current().details());
        System.out.println(ClassLayout.parseInstance(a).toPrintable());

//        System.out.println(ClassLayout.parseInstance(a).toPrintable());
//
//
//        synchronized (a) {
//            System.out.println("lock a");
//            System.out.println(ClassLayout.parseInstance(a).toPrintable());
//        }
//        # Field sizes by type: 4, 1, 1, 2, 2, 4, 4, 8, 8 [bytes]
//        [Oop(Ordinary Object Pointer), boolean, byte, char,short, int, float, long, double]大小

//        System.out.println("hashcode=" + a.hashCode());
//
//        System.out.println("after hash");
//
//        System.out.println(ClassLayout.parseInstance(a).toPrintable());


        //延迟偏向锁
        //-XX:BiasedLockingStartupDelay=20000 -XX:BiasedLockingStartupDelay=0


        //轻量级锁
//        long start = System.currentTimeMillis();
//
//        for (int i = 0; i < 1000000000L; i++) {
//            a.add();
//        }
//        long end = System.currentTimeMillis();
//
//        System.out.println(end-start);

        //2601  轻量级锁
        //3340  偏向锁

        //42875 重量级锁
        /**
        CountDownLatch downLatch = new CountDownLatch(1000000000);

        long start = System.currentTimeMillis();
        for (int i = 0; i < 2; i++) {
            new Thread(() -> {

                while (downLatch.getCount() > 0) {
                    a.add();
                    downLatch.countDown();
                }
            }).start();
        }


        downLatch.await();
        long end = System.currentTimeMillis();

        System.out.println(end - start);
*/


        System.out.println("before lock");
        System.out.println(ClassLayout.parseInstance(a).toPrintable());

        new Thread(() -> {

            synchronized (a){
                try {
                    Thread.sleep(5000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println("in sync lock");
                System.out.println(ClassLayout.parseInstance(a).toPrintable());
            }

        }).start();

        System.out.println("out sync lock");
        System.out.println(ClassLayout.parseInstance(a).toPrintable());

        synchronized (a){
            System.out.println("in sync");
        }

        System.out.println("haveny lock");
        System.out.println(ClassLayout.parseInstance(a).toPrintable());

        System.gc();

        System.out.println("after gc");
        System.out.println(ClassLayout.parseInstance(a).toPrintable());
    }
}
